package com.ttg.web.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.constant.BasicConstant;
import com.ttg.common.entities.BasePage;
import com.ttg.common.entities.UserTokenInfo;
import com.ttg.common.enums.AdminStatusEnum;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.BasicUtils;
import com.ttg.web.dao.AdminRoleDao;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.common.utils.StringUtil;
import com.ttg.common.utils.UserUtil;
import com.ttg.model.dto.*;
import com.ttg.model.mapper.AdminMapper;
import com.ttg.model.pojo.*;
import com.ttg.model.vo.*;
import com.ttg.web.dao.*;
import com.ttg.web.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/1/15 17:08
 */
@Slf4j
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    private String Lang = "zh-cn";
    private Pattern MOBILE_REG = Pattern.compile("^1[0-9]{10}$");

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminRoleAuthDao adminRoleAuthDao;
    @Autowired
    private AdminBindRoleDao adminBindRoleDao;
    @Autowired
    private AdminChannelDao adminChannelDao;
    @Autowired
    private AdminRoleLangDao adminRoleLangDao;
    @Autowired
    private AdminRoleDao adminRoleDao;
    @Autowired
    private AdminRoleHideDao adminRoleHideDao;

    @Autowired
    private AdminLogDao adminLogDao;

    @Override
    public AdminViewVo view(AdminViewDTO reqDto) {
        if (Objects.isNull(reqDto.getAdmId())){
            throw new BaseException(ErrorCodeEnums.MANAGER_NUM_NOT_NULL);
        }
        Admin admin = adminDao.getById(reqDto.getAdmId());
        if (Objects.isNull(admin)){
            throw new BaseException(ErrorCodeEnums.MANAGER_NOT_EXISTS);
        }
        AdminViewVo vo = new AdminViewVo();
        BeanUtils.copyProperties(admin, vo);
        vo.setAdmId(StringUtil.toStringOrNull(admin.getAdmId()));
        return vo;
    }

    @Override
    public AdminAdmIdVO delete(AdminDeleteDTO reqDto) {
        if (Objects.isNull(reqDto.getAdmId())){
            throw new BaseException(ErrorCodeEnums.MANAGER_NUM_NOT_NULL);
        }
        Admin admin = adminDao.getById(reqDto.getAdmId());
        if (Objects.isNull(admin)){
            throw new BaseException("删除管理员失败");
        }
        admin.setAdmStatus(AdminStatusEnum.TYPE_4.getValue());
        Long admId = admin.getAdmId();
        admin.setAdmId(admId);
//        String userName = BasicUtils.MD5(DateTimeUtil.formatYYYYMMDDHHMMSS(LocalDateTime.now()) + admId);
//        admin.setAdmUserName(userName);
        adminDao.updateById(admin);
        AdminAdmIdVO adminAdmIdVO = new AdminAdmIdVO();
        adminAdmIdVO.setAdmId(admin.getAdmId().toString());
        return adminAdmIdVO;
    }

    @Override
    public AdminAdmIdVO password(AdminPwdDTO reqDto) {
        UserTokenInfo userTokenInfo = UserUtil.getUserThrow();
        if (!reqDto.getAdmPassword().equals(reqDto.getPassword())){
            throw new BaseException(ErrorCodeEnums.PASSWORD_INCONSISTENT);
        }
        Long admId = reqDto.getAdmId() != null ? reqDto.getAdmId() : userTokenInfo.getAdmId();
        if (null == admId){
            throw new BaseException(ErrorCodeEnums.MANAGER_NOT_EXISTS);
        }
        Admin admin = adminDao.getById(admId);
        if (Objects.isNull(admin)){
            throw new BaseException(ErrorCodeEnums.MANAGER_NOT_EXISTS);
        }
        String oldPassword = DigestUtils.md5DigestAsHex(reqDto.getOldPassword().getBytes());
        if (!admin.getAdmPassword().equals(oldPassword)){
            throw new BaseException(ErrorCodeEnums.ORIGINAL_PASSWORD_ERROR);
        }
        String password = DigestUtils.md5DigestAsHex(reqDto.getAdmPassword().getBytes());
        admin.setAdmPassword(password);
        adminDao.saveOrUpdatePwd(admin);
        AdminAdmIdVO adminAdmIdVO = new AdminAdmIdVO();
        adminAdmIdVO.setAdmId(admin.getAdmId().toString());
        return adminAdmIdVO;
    }

    @Override
    public RoleStatusVO roleStatus(RoleStatusDTO reqDto) {
        int isHide = 0;
        if (Objects.isNull(reqDto.getArhRoleId())){
            throw new BaseException(ErrorCodeEnums.ROLE_NUM_NOT_NULL);
        }
        if(reqDto.getIsHide()==1) {
            //可见
            isHide = 1;
        }
        Integer hideCount = adminRoleHideDao.roleHideCount(reqDto.getArhRoleId());
        Long roleId;
        if (hideCount > 0){
            if (isHide == 0){
                adminRoleHideDao.delete(reqDto.getArhRoleId());
            }
            roleId = reqDto.getArhRoleId();
        }else {
            AdminRoleHide adminRoleHide = new AdminRoleHide();
            adminRoleHide.setArhRoleId(reqDto.getArhRoleId());
            adminRoleHideDao.saveOrUpdateAdmRoleHide(adminRoleHide);
            roleId = reqDto.getArhRoleId();
        }
        RoleStatusVO vo = new RoleStatusVO();
        vo.setArhRoleId(roleId.toString());
        return vo;
    }

    @Override
    public RoleIdVO roleAddsave(RoleAddSaveDTO reqDto) {
        if (Objects.isNull(reqDto.getRoleName())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(reqDto.getAuthId())){
            throw new BaseException(ErrorCodeEnums.AUTH_NOT_NULL);
        }
        Integer countByRoleName = adminRoleDao.getCountByRoleName(reqDto.getRoleName());
        if (countByRoleName>0){
            throw new BaseException(ErrorCodeEnums.ROLE_EXISTS);
        }
        AdminRole adminRole = new AdminRole();
        adminRole.setRoleName(reqDto.getRoleName());
        adminRole.setRoleType(BasicConstant.INT_1);
        adminRole.setRoleDesc(reqDto.getRoleDesc());
        List<Integer> list = reqDto.getAuthId();
        String roleAuths = StringUtils.join(list, ",");
        adminRole.setRoleAuths(roleAuths);
        adminRole.setRoleMctNo(0L);
        Long roleId = adminRoleDao.saveAdminRole(adminRole);
        for (Integer authId:list){
            AdminRoleAuth adminRoleAuth = new AdminRoleAuth();
            adminRoleAuth.setAraRoleId(roleId);
            adminRoleAuth.setAraAuthId(authId.longValue());
            adminRoleAuthDao.saveAuthId(adminRoleAuth);
        }
        RoleIdVO vo = new RoleIdVO();
        vo.setRoleId(roleId.toString());
        return vo;
    }

    @Override
    public RoleIdVO roleEditsave(RoleEditsaveDTO reqDto) {
        if (Objects.isNull(reqDto.getRoleId())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        if (Objects.isNull(reqDto.getRoleName())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        if (Objects.isNull(reqDto.getRoleType())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(reqDto.getAuthId())){
            throw new BaseException(ErrorCodeEnums.AUTH_NOT_NULL);
        }
        Integer countByRoleName = adminRoleDao.getCountByRoleIdAndRoleName(reqDto.getRoleId(),reqDto.getRoleName());
        if (countByRoleName>0){
            throw new BaseException(ErrorCodeEnums.ROLE_EXISTS);
        }
        AdminRole roleAuths = adminRoleDao.findRoleAuths(reqDto.getRoleId());
        if (Objects.isNull(roleAuths.getRoleAuths())){
            throw new BaseException(ErrorCodeEnums.ROLE_NOT_EXISTS);
        }
        AdminRole adminRole = new AdminRole();
        BeanUtils.copyProperties(reqDto, adminRole);
        List<Integer> list = reqDto.getAuthId();
        String auths = StringUtils.join(list, ",");
        adminRole.setRoleAuths(auths);
        adminRole.setRoleMctNo(0L);
        adminRoleDao.updateAdmRole(adminRole,reqDto.getRoleId());
        adminRoleAuthDao.deleteAuthIdByRoleId(reqDto.getRoleId());
        List<AdminRoleAuth> authList = new ArrayList<>();
        for (Integer authId:list){
            AdminRoleAuth adminRoleAuth = new AdminRoleAuth();
            adminRoleAuth.setAraRoleId(reqDto.getRoleId());
            adminRoleAuth.setAraAuthId(authId.longValue());
            authList.add(adminRoleAuth);
        }
        adminRoleAuthDao.saveBatch(authList);
        RoleIdVO vo = new RoleIdVO();
        vo.setRoleId(reqDto.getRoleId().toString());
        return vo;
    }

    @Override
    public RoleIdVO roleDelete(RoleDeleteDTO reqDto) {
        if (Objects.isNull(reqDto.getRoleId())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        Integer countByRoleId = adminRoleDao.getCountByRoleId(reqDto.getRoleId());
        if (countByRoleId<=0){
            throw new BaseException(ErrorCodeEnums.ROLE_NOT_EXISTS);
        }
        if (0<adminDao.countByAdmRoleId(reqDto.getRoleId())){
            throw new BaseException(ErrorCodeEnums.ROLE_IS_USED);
        }
        adminRoleDao.deleteByRoleId(reqDto.getRoleId());
        adminRoleAuthDao.deleteAuthIdByRoleId(reqDto.getRoleId());
        RoleIdVO vo = new RoleIdVO();
        vo.setRoleId(reqDto.getRoleId().toString());
        return vo;
    }

    @Override
    public BasePage<RoleListVO> roleList(RoleListDTO reqDto) {
        BasePage<AdminRole> basePage = adminRoleDao.selectListByKeywordAndRoleTypeAndOrgNo(reqDto);
        BasePage<RoleListVO> voPage = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal(), basePage.isSearchCount());
        List<AdminRole> list = basePage.getList();
        List<RoleListVO> voList = new ArrayList<>();
        for (AdminRole adminRole : list){
            RoleListVO vo = new RoleListVO();
            BeanUtils.copyProperties(adminRole, vo);
            vo.setRoleId(StringUtil.toStringOrNull(adminRole.getRoleId()));
            vo.setRoleOrgNo(StringUtil.toStringOrNull(adminRole.getRoleOrgNo()));
            AdminRoleLang arl = adminRoleLangDao.getArlByRoleId(adminRole.getRoleId(),Lang);
            if (Objects.nonNull(arl)&&Objects.nonNull(arl.getArlRoleName())){
                vo.setRoleName(arl.getArlRoleName());
            }
            AdminRoleHide arh = adminRoleHideDao.getArhByRoleId(adminRole.getRoleId());
            vo.setIsHide(0);
            if (Objects.nonNull(arh) && arh.getArhId()>0){
                vo.setIsHide(1);
            }
            voList.add(vo);
        }
        voPage.setList(voList);
        return voPage;
    }

    @Override
    public List<RoleAuthVO> roleAuth() {
        List<RoleAuthResVO> roleAuthResVos = adminChannelDao.roleAuthByTypeList(Lang);
        Map<Long,List<RoleAuthsVO>> map = new HashMap<>(12);
        for (RoleAuthResVO resVO : roleAuthResVos){
            List<RoleAuthsVO> resVOList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(map.get(resVO.getChlId()))){
                resVOList = map.get(resVO.getChlId());
            }
            RoleAuthsVO authResVO = new RoleAuthsVO();
            authResVO.setAuthId(resVO.getAuthId().toString());
            authResVO.setAuthName(resVO.getAuthName());
            resVOList.add(authResVO);
            map.put(resVO.getChlId(),resVOList);
        }
        List<AdminChannel> adminChannels = adminChannelDao.list();
        List<RoleAuthVO> voList = new ArrayList<>();
        for (AdminChannel adminChannel : adminChannels){
            RoleAuthVO vo = new RoleAuthVO();
            vo.setChlId(adminChannel.getChlId().toString());
            vo.setChlName(adminChannel.getChlName());
            vo.setAuths(map.get(adminChannel.getChlId()));
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public RoleViewVO roleView(RoleViewDTO reqDto) {
        if (Objects.isNull(reqDto.getRoleId())){
            throw new BaseException(ErrorCodeEnums.ROLE_NAME_NOT_NULL);
        }
        AdminRole adminRole = adminRoleDao.selectOneByIdAndOrgNO(reqDto.getRoleId());
        if (Objects.isNull(adminRole)){
            throw new BaseException(ErrorCodeEnums.SYSTEM_ERROR.getErrcode(),"角色不存在");
        }
        RoleViewVO vo = new RoleViewVO();
        vo.setRoleId(StringUtil.toStringOrNull(adminRole.getRoleId()));
        vo.setRoleName(adminRole.getRoleName());
        vo.setRoleType(adminRole.getRoleType());
        vo.setRoleDesc(adminRole.getRoleDesc());
        String[] split = adminRole.getRoleAuths().split(",");
        List<RoleAuthResVO> roleAuthResVOS = adminChannelDao.roleAuthByTypeList(Lang);
        Map<Long,List<RoleViewChlAuthsVO>> map = new HashMap<>();
        for (RoleAuthResVO resVO : roleAuthResVOS){
            List<RoleViewChlAuthsVO> resVOList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(map.get(resVO.getAuthId()))){
                resVOList = map.get(resVO.getAuthId());
            }
            RoleViewChlAuthsVO authResVO = new RoleViewChlAuthsVO();
            BeanUtils.copyProperties(resVO,authResVO);
            resVOList.add(authResVO);
            map.put(resVO.getAuthId(),resVOList);
        }
        List<RoleViewChlAuthsVO> auths = new ArrayList<>();
        Set<String> ids = Arrays.stream(split).collect(Collectors.toSet());
        for (String id : ids){
            if (Objects.nonNull(id)) {
                if (CollectionUtils.isEmpty(auths)){
                    auths = map.get(Long.valueOf(id));
                }
                if (!CollectionUtils.isEmpty(map.get(Long.valueOf(id)))) {
                    auths.addAll(map.get(Long.valueOf(id)));
                }
            }
        }
        Set<RoleViewChlAuthsVO> set = new HashSet<>(auths);
        vo.setAuths(set);
        return vo;
    }

    @Override
    public BasePage<AdminIndexVO> index(AdminIndexDTO reqDto) {
        Integer status = null;
        if (Objects.nonNull(reqDto.getAdmStatus())){
            status = reqDto.getAdmStatus();
        }
        BasePage<AdminIndexVO> page = reqDto.parse();
        BasePage<AdminIndexVO> voPage = adminDao.adminIndexGetPage(page,status,reqDto.getAdmId(),reqDto.getTrueName(),reqDto.getUserName());
        return voPage;
    }

    @Override
    public List<RoleVO> role() {
        List<AdminRoleHide> arhByOrg = adminRoleHideDao.list();
        List<Long> roleHideId = new ArrayList<>();
        for (AdminRoleHide arh : arhByOrg){
            Long id = arh.getArhRoleId();
            roleHideId.add(id);
        }
        List<RoleVO> role = adminRoleDao.role(Lang, roleHideId);
        Set set = new HashSet();
        set.addAll(role);
        List<RoleVO> reVo = new ArrayList<>();
        reVo.addAll(set);
        return reVo;
    }

    @Override
    public AdminAddSaveVO addSave(AdminAddSaveDTO reqDto) {
        if (StringUtils.isBlank(reqDto.getAdmUserName())){
            throw new BaseException(ErrorCodeEnums.USER_ACCOUNT_NOT_NULL);
        }
        if (StringUtils.isBlank(reqDto.getAdmTrueName())){
            throw new BaseException(ErrorCodeEnums.REAL_NAME_NOT_NULL);
        }
        if (StringUtils.isBlank(reqDto.getAdmPassword())){
            throw new BaseException(ErrorCodeEnums.USER_PASSWORD_NOT_NULL);
        }
        if (reqDto.getAdmPassword().length() != 40){
            throw new BaseException(ErrorCodeEnums.PASSWORD_LENGTH_ERROR);
        }
        if (Objects.isNull(reqDto.getAdmStatus())){
            throw new BaseException(ErrorCodeEnums.USER_STATUS_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(reqDto.getRoles())){
            throw new BaseException(ErrorCodeEnums.USER_ROLE_NOT_NULL);
        }
        if (StringUtils.isBlank(reqDto.getAdmMobile())) {
            throw new BaseException("手机号不能为空");
        }
        if (!StringUtil.checkPhone(reqDto.getAdmMobile(),Lang)){
            throw new BaseException("手机号码格式错误");
        }
        if (StringUtils.isNotBlank(reqDto.getAdmEmail()) && !StringUtil.checkEmail(reqDto.getAdmEmail())){
            throw new BaseException("电子邮件格式错误");
        }

        Admin admByUserName1 = adminDao.findAdmByUserName(reqDto.getAdmUserName());
        if (Objects.nonNull(admByUserName1)) {
            if (!Objects.equals(AdminStatusEnum.TYPE_4.getValue(),admByUserName1.getAdmStatus())){
                throw new BaseException(ErrorCodeEnums.USER_ACCOUNT_EXISTS);
            }else {
                throw new BaseException(ErrorCodeEnums.USER_ACCOUNT_IS_EXISTS);
            }

        }


        List<String> roleName = new ArrayList<>();
        for (Long roleId : reqDto.getRoles()){
            AdminRole roleById = adminRoleDao.findRoleById(roleId);
            AdminRoleLang roleByRoleId = adminRoleLangDao.findRoleByRoleIdAndLang(roleId,Lang);
            String name = Objects.nonNull(roleByRoleId) ? roleByRoleId.getArlRoleName() : roleById.getRoleName();
            roleName.add(name);
        }
        String strRoleName = StringUtils.strip(roleName.toString(),"[]").replace(" ", "");
        String strRoleId = StringUtils.strip(reqDto.getRoles().toString(),"[]").replace(" ", "");
        Admin admin = new Admin();
        admin.setAdmUserName(reqDto.getAdmUserName());
        admin.setAdmTrueName(reqDto.getAdmTrueName());
        admin.setAdmMobile(reqDto.getAdmMobile());
        admin.setAdmEmail(reqDto.getAdmEmail());
        admin.setAdmPassword(DigestUtils.md5DigestAsHex(reqDto.getAdmPassword().getBytes()));
        admin.setAdmStatus(reqDto.getAdmStatus());
        admin.setAdmRoleId(strRoleId);
        admin.setAdmRoleName(strRoleName);
        admin.setAdmAddTime(LocalDateTime.now());
        admin.setAdmErrTimes(0);
        Admin adm = adminDao.saveAdm(admin);
        Admin admByUserName = adminDao.findAdmByUserName(adm.getAdmUserName());
        Long admId = admByUserName.getAdmId();
        for (Long role : reqDto.getRoles()){
            AdminBindRole abr = new AdminBindRole();
            abr.setAbrAdmId(admId);
            abr.setAbrRoleId(role);
            adminBindRoleDao.save(abr);
        }
        AdminAddSaveVO vo = new AdminAddSaveVO();
        vo.setAdmId(admId.toString());
        return vo;
    }

    @Override
    public boolean editSave(AdminEditSaveDTO reqDto) {
        String pwd = null;
        if (Objects.isNull(reqDto.getAdmId())){
            throw new BaseException(ErrorCodeEnums.MANAGER_NUM_NOT_NULL);
        }
        if (Objects.isNull(reqDto.getAdmTrueName())){
            throw new BaseException(ErrorCodeEnums.REAL_NAME_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(reqDto.getRoles())){
            throw new BaseException(ErrorCodeEnums.USER_ROLE_NOT_NULL);
        }
        if (Objects.nonNull(reqDto.getAdmPassword())){
            if (reqDto.getAdmPassword().length() != 40){
                throw new BaseException(ErrorCodeEnums.PASSWORD_LENGTH_ERROR);
            }
            pwd = DigestUtils.md5DigestAsHex(reqDto.getAdmPassword().getBytes());
        }
        if (Objects.isNull(reqDto.getAdmStatus())){
            throw new BaseException(ErrorCodeEnums.USER_STATUS_NOT_NULL);
        }
        if (StringUtils.isBlank(reqDto.getAdmMobile())) {
            throw new BaseException("手机号不能为空");
        }
        if (!StringUtil.checkPhone(reqDto.getAdmMobile(),Lang)){
            throw new BaseException("手机号码格式错误");
        }
        if (Objects.nonNull(reqDto.getAdmEmail()) && !StringUtil.checkEmail(reqDto.getAdmEmail())){
            throw new BaseException("电子邮件格式错误");
        }
        List<String> roleName = new ArrayList<>();
        List<AdminRole> adminRoles = adminRoleDao.selectListByRoleIds(reqDto.getRoles());
        List<Long> roleIds = adminRoles.stream().map(AdminRole::getRoleId).collect(Collectors.toList());
        for (Long roleId : roleIds) {
            AdminRole roleById = adminRoleDao.findRoleById(roleId);
            AdminRoleLang roleByRoleId = adminRoleLangDao.findRoleByRoleIdAndLang(roleId,Lang);
            String name = Objects.nonNull(roleByRoleId) ? roleByRoleId.getArlRoleName() : roleById.getRoleName();
            roleName.add(name);
        }
        String strRoleName = null;
        if (!CollectionUtils.isEmpty(roleName)) {
            strRoleName = StringUtils.strip(roleName.toString(), "[]").replace(" ", "");
        }
        String strRoleId = null;
        if (!CollectionUtils.isEmpty(roleIds)) {
            strRoleId = StringUtils.strip(roleIds.toString(), "[]").replace(" ", "");
        }
        Integer errTimes = null;
        if (AdminStatusEnum.TYPE_1.getValue().equals(reqDto.getAdmStatus())) {
            errTimes = 0;
        }
        Admin admin = new Admin();
        admin.setAdmId(reqDto.getAdmId());
        admin.setAdmTrueName(reqDto.getAdmTrueName());
        admin.setAdmPassword(pwd);
        admin.setAdmStatus(reqDto.getAdmStatus());
        admin.setAdmErrTimes(errTimes);
        admin.setAdmRoleId(strRoleId);
        admin.setAdmRoleName(strRoleName);
        admin.setAdmMobile(reqDto.getAdmMobile());
        admin.setAdmEmail(reqDto.getAdmEmail());
        boolean update = adminDao.updateById(admin);
        if (update) {
            AdminBindRole adminBindRole = new AdminBindRole();
            adminBindRole.setAbrAdmId(reqDto.getAdmId());
            adminBindRoleDao.delete(adminBindRole);
            for (Long role : reqDto.getRoles()) {
                AdminBindRole abr = new AdminBindRole();
                abr.setAbrAdmId(reqDto.getAdmId());
                abr.setAbrRoleId(role);
                adminBindRoleDao.save(abr);
            }
        }
        return true;
    }

    @Override
    public ManageAdminLogVO log(ManageAdminLogDTO reqDto) {
        List<AdminChannel> adminChannels = adminChannelDao.findChannelName(BasicConstant.LANG_ZH_CN);
        ManageAdminLogVO vo = new ManageAdminLogVO();
        List<ManageAdminLogChannelVO> adminLogChannelVOS = new ArrayList<>();
        //获取系统频道列表
        if (!CollectionUtils.isEmpty(adminChannels)){
            for (AdminChannel adminChannel : adminChannels) {
                ManageAdminLogChannelVO channelVO = new ManageAdminLogChannelVO();
                channelVO.setChlId(adminChannel.getChlId());
                channelVO.setChlName(adminChannel.getChlName());
                adminLogChannelVOS.add(channelVO);
            }
        }
        ManageAdminLogChannelVO adminChannel = new ManageAdminLogChannelVO();
        adminChannel.setChlId(-2L);
        adminChannel.setChlName(null);
        adminLogChannelVOS.add(adminChannel);
        vo.setChlList(adminLogChannelVOS);
        //查询条件
        Long admId = null;
        String admUserName = null;
        String admTrueName = null;
        Long logObjId = null;
        List<Long> logChlIds = new ArrayList<>();
        LocalDate sDate = null;
        LocalDate eDate = null;
        if (Objects.nonNull(reqDto.getAdmId())){
            admId = reqDto.getAdmId();
        }
        if (StringUtils.isNotBlank(reqDto.getUserName())){
            admUserName = reqDto.getUserName()+"%";
        }
        if (StringUtils.isNotBlank(reqDto.getTrueName())){
            admTrueName = reqDto.getTrueName()+"%";
        }
        if (Objects.nonNull(reqDto.getObjId())){
            logObjId = reqDto.getObjId();
        }
        if (StringUtils.isNotBlank(reqDto.getKeyWord())){
            List<Long> chlIds = adminChannelDao.findChlIdByChannelName(reqDto.getKeyWord(), BasicConstant.LANG_ZH_CN);
            if (!CollectionUtils.isEmpty(chlIds)){
                logChlIds = chlIds;
            }else {
                logChlIds.add(-1L);
            }
        }
        if (StringUtils.isNotBlank(reqDto.getSdate()) && StringUtils.isNotBlank(reqDto.getEdate())){
            eDate = DateTimeUtil.strToDateYYYY_MM_DD(reqDto.getEdate()).plusDays(1);
            sDate = DateTimeUtil.strToDateYYYY_MM_DD(reqDto.getSdate());
        }else if (StringUtils.isNotBlank(reqDto.getSdate())){
            sDate = DateTimeUtil.strToDateYYYY_MM_DD(reqDto.getSdate());
        }else if (StringUtils.isNotBlank(reqDto.getEdate())){
            eDate = DateTimeUtil.strToDateYYYY_MM_DD(reqDto.getEdate());
        }
        //分页
        BasePage<AdminLog> parse = reqDto.parse();
        BasePage<AdminLog> resBasePage = adminLogDao.findAdminLogPage(parse,admId,admUserName,admTrueName,logObjId,logChlIds,sDate,eDate,reqDto.getAdmIds());
        BasePage<ManageAdminLogPageVO> voBasePage = new BasePage<>(parse.getCurrent(),parse.getSize(),parse.getTotal(),parse.isSearchCount());
        List<AdminLog> resList = resBasePage.getList();
        List<ManageAdminLogPageVO> voList = new ArrayList<>();
        for (AdminLog resVO : resList){
            ManageAdminLogPageVO admLog = new ManageAdminLogPageVO();
            BeanUtils.copyProperties(resVO,admLog);
            voList.add(admLog);
        }
        voBasePage.setList(voList);
        vo.setPage(voBasePage);
        return vo;
    }


}
